package st.business.tepth.business.user.busnumber.workholder.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.jakewharton.rxbinding2.widget.RxTextView;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Predicate;
import st.business.tepth.R;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.view.ClearEditText;
import st.shadow.library.core.util.utilcode.util.ScreenUtils;

/**
 * @author zhangyu
 * @date 2019/2/19
 * 耗材用量
 */
public class DialogNumberSelect extends Dialog {
    public DialogNumberSelect(Context context,
                              final OnItemChooseListener<String> mListener) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_good_count, null);
        ClearEditText clearEditText = view.findViewById(R.id.et_count);
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        final TextView tvConfirm = view.findViewById(R.id.tv_confirm);
        RxTextView.textChanges(clearEditText)
                .subscribeOn(AndroidSchedulers.mainThread())
                .debounce(600, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
                .filter(new Predicate<CharSequence>() {
                    @Override
                    public boolean test(final CharSequence charSequence) throws Exception {
                        boolean isEnable = charSequence.length() > 0;
                        tvConfirm.setBackgroundResource(isEnable ?
                                R.drawable.shape_btn_operation_n :
                                R.drawable.shape_btn_operation_t);
                        tvConfirm.setTextColor(isEnable ? Color.parseColor("#ffffff") :
                                Color.parseColor("#333333"));
                        tvConfirm.setEnabled(isEnable);
                        tvConfirm.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mListener.onChoose(charSequence.toString());
                                dismiss();
                            }
                        });
                        return charSequence.length() > 0;
                    }
                }).subscribe(new Observer<CharSequence>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(CharSequence charSequence) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
        setContentView(view);
    }

    public Dialog measureSize() {
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = ScreenUtils.getScreenWidth() * 1000 / 1280; // 宽度
            lp.height = ScreenUtils.getScreenHeight() * 420 / 1024; // 高度
            dialogWindow.setGravity(Gravity.CENTER);
            dialogWindow.setAttributes(lp);
        }
        return this;
    }

}
